function checkExist(objMain,objMultiList) // function cheching where id already existing or not
{
	//var district=document.getElementById('district1').value;
	//var obj=document.getElementById('multi');
	for (var i = 0;i<objMultiList.length;i++) 
	{
		if(objMain.value==objMultiList.options[i].value)
		{
			alert("The chosen district is already added.");
			return false;
		}
	}
	return true;
}

function createDropdown(data,objMain,objMultiList) //function will be fired when click on add button
{
	//alert(objMultiList.value);
	//for checing If the district has been chosen already....
	if(checkExist(objMain,objMultiList))
	{
		
		var selIndex = objMain.selectedIndex;
		var selText=objMain.options[selIndex].text;
		//var district=document.getElementById('district1').value;
		//var obj=document.getElementById('multi');
		//var obj1=document.getElementById('portfolio');
		if(objMain.value == "" ) 
		{
			alert("Please select any district ");
			//return false;
		}

		opt = document.createElement("OPTION");
		//alert(opt);
		opt.value = objMain.value;
		opt.text=selText;
		opt.visible=true;
		//opt.selected=true;
		//objMultiList.add(opt,null);
		objMultiList.add(opt,null);	
			
		if(data=="block2")
		{
		changeBlock2(data);
		}
		if(data=="panchayat2")
		{
		changePanchayat2(data);
		}
		if(data=="village2")
		{
		changeVillage2(data);
		}
		if(data=="habit")
		{
		changeHabit(data);
		}
		//getvariable();
	}
}

function removeOption(data,valmulti) //function will be fired when click on remove button
{
	//alert(valmulti.selectedIndex);
	if(valmulti.selectedIndex<0)
	{
		alert ("Please select any option ");
		//return false;
	}
	else 
	{
		var lengtharr=valmulti.length;
		//alert(lengtharr);
		var arrdistrict = new Array(lengtharr);
		for (var i=valmulti.options.length-1;i>=0;i--) 
		{
			arrdistrict[i]=valmulti.options[i].value;
			if(valmulti.options[i].selected)
			{
				//alert(valmulti.options[i].selected);
				valmulti.remove(i);
		if(data=="block2")
		{
		changeBlock2(data);
		}
		/*if(data=="tehsil")
		{
		changeTehsil(data);
		}*/
		if(data=="panchayat2")
		{
		changePanchayat2(data);
		}
		if(data=="village2")
		{
		changeVillage2(data);
		}
		if(data=="habit")
		{
		changeHabit(data);
		}
		//getvariable();
			}
		
		}
	 
	}
}




// JavaScript Document
function Inint_AJAX() 
{
	try 
	{ 
		return new ActiveXObject("Msxml2.XMLHTTP");  
	} 
	catch(e) 
	{} //IE
	try 
	{ 
		return new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	catch(e) {} //IE
	try 
	{ 
		return new XMLHttpRequest(); 
	} catch(e) {} //Native Javascript
	alert("XMLHttpRequest not supported");
	return null;
};
		
function changeDistrict2(district2,val) 
{
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('district2').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+district2+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}
window.onLoad=changeDistrict2('district2',-1);

function changeBlock2(block2) 
{
	var val="";
    var obj=document.getElementById('multiDist');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('blockLoader2').innerHTML="<img src='images/ajax-loader.gif' alt='image' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('blockLoader2').innerHTML=" ";
				document.getElementById('block2').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+block2+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	//changeTehsil("tehsil") 
}

/*function changeTehsil(tehsil) 
{
	var val="";
    var obj=document.getElementById('multiDist');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('tehsilLoader').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('tehsilLoader').innerHTML=" "; 
				document.getElementById('tehsil').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("GET", "dropdown_ufw.php?data="+tehsil+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function getvariable() {
   var valTehsil="";
    var obj=document.getElementById('multiTehsil');
	
	for (var i = 0;i<obj.length;i++) 
	{
			 valTehsil = valTehsil+" " +obj.options[i].value;
			document.questionnaire1.mulTehsil.value = valTehsil; 
	}
}*/

function changePanchayat2(panchayat2) 
{
	var val="";
    var obj=document.getElementById('multiBlock');
	for (var i = 0;i<obj.length;i++) 
	{
			 val = val + " " + obj.options[i].value;
	}
	//alert(val);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('panchayatLoader2').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('panchayatLoader2').innerHTML=" "; 
				document.getElementById('panchayat2').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+panchayat2+"&val="+val,false); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changeVillage2(village2) 
{
	
	var val="";
    var obj=document.getElementById('multiPanchayat');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		document.getElementById('villageLoader2').innerHTML="<img src='images/ajax-loader.gif' />";
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('villageLoader2').innerHTML=" ";
				document.getElementById('village2').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+village2+"&val="+val,false); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	
}

function changeHabit(habit) 
{
	
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		val = val + " " + obj.options[i].value;
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('habit').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+habit+"&val="+val,false); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
	/*changePopulation("population_benefited");
	changePrePopulation("present_population");
	change_fut_Population("future_population");*/
	changeCensusCode("cvc");
}

function changeCensusCode(cvc) 
{
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
			 val = val + " " + obj.options[i].value;
	}
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('cvc').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("post", "dropdown_ufw.php?data="+cvc+"&val="+val,false); 

	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changeFormId(form,form_id,val) 
{
	//var dis_name = form.district.options[form.district.selectedIndex].text;
	//alert(dis_name);
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
	   //alert(req.readyState);
		if (req.readyState == 4)
		{
			//alert(req.status);
			if (req.status == 200) 
			{
				document.getElementById('form_id').innerHTML = req.responseText; 
			}
		}
	};
	var dis_name = form.district2.options[form.district2.selectedIndex].text;
	req.open("post", "dropdown_ufw.php?dist="+dis_name+"&data="+form_id+"&val="+val,false);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

/*function changePopulation(population_benefited) 
{
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('population_benefited').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("GET", "dropdown_ufw.php?data="+population_benefited+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function changePrePopulation(present_population) 
{
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('present_population').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("GET", "dropdown_ufw.php?data="+present_population+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function change_fut_Population(future_population) 
{
	var val="";
    var obj=document.getElementById('multiVillage');
	for (var i = 0;i<obj.length;i++) 
	{
		
			 val = val + " " + obj.options[i].value;
		
	}
	
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('future_population').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("GET", "dropdown_ufw.php?data="+future_population+"&val="+val); 
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}

function change_factor(factor, val) 
{
	var req = Inint_AJAX();
	req.onreadystatechange = function () 
	{ 
		if (req.readyState==4)
		{
			if (req.status==200) 
			{
				document.getElementById('factor').innerHTML=req.responseText; 
							//alert(req.responseText);
			} 
		}
	};
	req.open("GET", "dropdown_ufw.php?data="+factor+"&val="+val);
	//alert("dropdown_q2.php?data="+factor+"&val="+val);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=tis-620"); // set Header
	req.send(null); 
}*/